package net.oschina.app.ui;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import net.oschina.app.R;
import net.oschina.app.base.BaseActivity;
import net.oschina.app.base.BaseProtocol;
import net.oschina.app.bean.TweetPubBean;
import net.oschina.app.util.CommonUtils;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;

/**
 * Created by Administrator on 2017/1/16.
 * 文字界面
 */
public class TweetPubActivity extends BaseActivity {

    @BindView(R.id.ev_content)
    EditText mEvContent;
    @BindView(R.id.tv_sizeCount)
    TextView mTvSizeCount;
    @BindView(R.id.rl)
    RelativeLayout mRl;
    @BindView(R.id.iv_pic)
    ImageView mIvPic;
    @BindView(R.id.iv_at)
    ImageView mIvAt;
    @BindView(R.id.iv_soft)
    ImageView mIvSoft;
    @BindView(R.id.iv_emoji)
    ImageView mIvEmoji;
    @BindView(R.id.ll_tweet)
    LinearLayout mLlTweet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);
        ButterKnife.bind(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.pub_new_team_active_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.public_menu_send:
//                Toast.makeText(this, "fasong", Toast.LENGTH_SHORT).show();
                pubTweet();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void pubTweet() {
        final String content = mEvContent.getText().toString().trim();
         new Thread(new Runnable() {

             private String mMessage;

             @Override
             public void run() {
                 BaseProtocol baseProtocol = new BaseProtocol<TweetPubBean>() {
                     @Override
                     protected Call<TweetPubBean> getCall(String pageIndex) {
                         return null;
                     }
                 };
                 Call<TweetPubBean> tweetPubBean = baseProtocol.getGsonService().getTweetPubBean(content);
                 try {
                     mMessage = tweetPubBean.execute().body().getMessage();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
                 CommonUtils.runOnMainThread(new Runnable() {
                     @Override
                     public void run() {
                         Toast.makeText(TweetPubActivity.this, mMessage, Toast.LENGTH_SHORT).show();
                     }
                 });
                 finish();
             }
         }).start();
    }
}
